<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - collada - skinning</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				color: #000;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}

			a {

				color: #f00;
			}

		</style>
	</head>
	<body>

		<div id="container"></div>

		<div id="info">
		<a href="http://threejs.org" target="_blank">three.js</a> webgl - collada - skinning
		</div>

		<script src="../build/three.js"></script>
		<script src="js/loaders/collada/Animation.js"></script>
		<script src="js/loaders/collada/AnimationHandler.js"></script>
		<script src="js/loaders/collada/KeyFrameAnimation.js"></script>
		<script src="js/loaders/ColladaLoader.js"></script>
		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;
			var camera, scene, renderer;
			var clock = new THREE.Clock();

			init();

			function init() {

				container = document.getElementById( 'container' );

				camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 1, 10000 );
				camera.position.set( -5, -5, 5 );
				camera.up.set( 0, 0, 1 );

				scene = new THREE.Scene();

				var light = new THREE.DirectionalLight( 0xffffff, 1.5 );
				light.position.set( 0, -4, -4 ).normalize();
				scene.add( light );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setClearColor( 0xfff4e5 );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.sortObjects = false;
				container.appendChild( renderer.domElement );

				stats = new Stats();
				container.appendChild( stats.dom );
				

				var loader = new THREE.ColladaLoader();
				loader.load( "./models/collada/avatar.dae", function ( collada ) {
				
					collada.scene.traverse( function ( child ) {

						if ( child instanceof THREE.SkinnedMesh ) {

							var animation = new THREE.Animation( child, child.geometry.animation );
							animation.play();

							camera.lookAt( child.position );

						}

					} );

					scene.add( collada.scene );

				} );

				window.addEventListener( 'resize', onWindowResize, false );

				animate();

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate, renderer.domElement );

				THREE.AnimationHandler.update( clock.getDelta() );

				renderer.render( scene, camera );

				stats.update();

			}

		</script>

	</body>
</html>

